const net = require('net');
const utils = require('./src/utils');

const HOST = '47.114.179.84';
// const HOST = '0.0.0.0';
const PORT = 1021;

const tcpClient = net.Socket();

function randomString(len) {
　　len = len || 32;
　　var $chars = 'ABCDE12345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
　　var maxPos = $chars.length;
　　var pwd = '';
　　for (i = 0; i < len; i++) {
　　　　pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
　　}
　　return pwd;
}

const mac = randomString(12);

tcpClient.connect(PORT, HOST, function () {
   tcpClient.write(utils.str2ArrayBuffer("7b0002a401a77d1a"));
   setInterval(() => {
      tcpClient.write(utils.str2ArrayBuffer("7b0002a401a77d1a"));
   }, 5000)
});

tcpClient.on('data', function (data) {
   const str = utils.bytes2Str(data);
   if (str === '7b:00:02:a2:02:a6:7d') {
      tcpClient.write(utils.str2ArrayBuffer(`7b0008a202${mac}7e7d1a`)); //服务器向客户端发送消息
   } else if (str === '7b:00:02:a2:01:a5:7d') {
      tcpClient.write(utils.str2ArrayBuffer('7b0016a2013132312e3538383233362c33382e393838303232bd7d1a'));//服务器向客户端发送消息
      setTimeout(() => {
         tcpClient.write(utils.str2ArrayBuffer('4f4b1a'));//服务器向客户端发送消息
      }, 500)
   }
})
